package com.iscas.lndicatormonitor.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iscas.lndicatormonitor.common.CustomResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class deal {

        @GetMapping("/deal")
    public CustomResult getAllTaskByPage(@RequestBody String rawData){

            JSONObject jsonObject = JSONObject.parseObject(rawData);

            JSONArray nodesArray = jsonObject.getJSONObject("applicationMapData").getJSONArray("nodeDataArray");
            JSONArray linksArray = jsonObject.getJSONObject("applicationMapData").getJSONArray("linkDataArray");

            JSONArray nodesResult = new JSONArray();
            JSONArray edgesResult = new JSONArray();

            for (Object nodeObj : nodesArray) {
                JSONObject node = (JSONObject) nodeObj;
                JSONObject nodeResult = new JSONObject();
                nodeResult.put("id", node.getString("key"));
                nodeResult.put("label", node.getString("applicationName"));
                nodeResult.put("type", node.getString("category"));

                nodesResult.add(nodeResult);
            }

            for (Object linkObj : linksArray) {
                JSONObject link = (JSONObject) linkObj;
                JSONObject edgeResult = new JSONObject();
                edgeResult.put("source", link.getString("from"));
                edgeResult.put("target", link.getString("to"));

                edgesResult.add(edgeResult);
            }

            JSONObject result = new JSONObject();
            result.put("nodes", nodesResult);
            result.put("edges", edgesResult);

            return CustomResult.ok(result);

    }
}
